/**
 * Created by dale on 2017/8/2.
 */
var currentDepId;
var dataList;
var currentDepName;
var checkId;
$(document).ready(function (){
    var  depList= $('#depList');
    dataList=$('#dataList');
    $('.xh-datagrid-toolbar').parseToolbar();

    dataList.initDataGrid('getAppReleasePlanData').setSelectThenEnable([]);

    $('#cc').resizable({    //左侧拖曳
        onStopResize:function (e) {
            depList.treegrid('resize',{width:e['data']['width']});
            $('.xh-datagrid').attr('width',($(document).width()-e['data']['width']));
            dataList.datagrid('resize',{width:($(document).width()-e['data']['width'])})
        },
        onResize:function (e) {
            depList.treegrid('resize',{width:e['data']['width']}+'px');
            dataList.datagrid('resize',{width:($(document).width()-e['data']['width'])})
        }
    });

    depList.treegrid({
        selectOnCheck:false,
        checkOnSelect:false,
        onSelect:function (row) {
            dataList.dataGridClear();
            currentDepId=row['iDepId'];
            currentDepName=row['sDepName'];
            currentDeptType=row['deptType'];//节点类型，1父节点2子节点
            currentTableType=row['tableType'];//table类型，1按应用2按时间

            depList.treegrid('expand',currentDepId);
            $('#bt_list_add').setButtonEnable('eu');
            $('#bt_list_import').setButtonEnable('eu');
            if (currentDeptType === 2){
                dataList.addDataGridParams(getParams()).setSelectThenEnable(['bt_list_agree', 'bt_list_deleted']).datagrid('getPanel').panel('setTitle', '学校列表（' + row['sDepName'] + '）');
            }else{
                dataList.addDataGridParams(getParams()).setSelectThenEnable([]).datagrid('getPanel').panel('setTitle', '学校列表（' + row['sDepName'] + '）');
            }
        },
        onBeforeCheck:function (row) {
            if(checkId==undefined){
                checkId = row['iDepId'];
                return true;
            }else {
                if(checkId==row['iDepId']){
                    depList.treegrid('uncheckRow',row['iDepId']);
                    return false;
                }else {
                    depList.treegrid('uncheckRow',checkId);
                }
            }
        },
        onCheck:function (row) {
            checkId = row['iDepId'];
            $('#bt_list_agree').setButtonDisable('eu');
            $('#bt_list_deleted').setButtonDisable('eu');
        },
        onLoadSuccess:function (row, data) {
            depList.treegrid('select',0);
        }

    });

    DataGrid.onLoadSuccess = function(data){
        dataList.dataGridClear();
    };

    $('#bt_list_search').click(function () {
        var key=xhExtra.getValue($('#list_search'));
        dataList.addDataGridParams({key:key,depId:currentDepId,depttype:currentDeptType});
    });
    $('#bt_list_agree').click(function () {
        if(xhExtra.buttonDisabled($(this))){
            return false;
        }
        var ids = dataList.getDataGridSelectedFields('iId');
        window.top.swal({
            title: '确定要通过吗？',
            text: '',
            type: 'warning',
            showCancelButton: true,
            confirmButtonText: '确定',
            cancelButtonText: '取消',
        }).then(function (result) {
            $.ajax({
                url:baseUrl+'Apprelease/agree',
                data:{ids:ids},
                type:'POST',
                success:function (data) {
                    window.top.swal(
                        data['msg'],
                        '',
                        'success'
                    );
                    dataList.dataGridReload();
                },
                error:function (data) {
                    window.top.swal(
                        data['responseJSON']['error'],
                        '',
                        'error'
                    );
                }
            });
        },function () {
        })
    });
    $('#bt_list_deleted').click(function () {
        if(xhExtra.buttonDisabled($(this))){
            return false;
        }
        var id = dataList.getDataGridSelectedField('id');
        var schoolIds = dataList.getDataGridSelectedFields('iSchoolId');
        window.top.swal({
            title: '确定要删除吗？',
            text: '',
            type: 'warning',
            showCancelButton: true,
            confirmButtonText: '确定',
            cancelButtonText: '取消',
        }).then(function (result) {
            $.ajax({
                url:baseUrl+'Apprelease/deleteSchool',
                data:{id:id, schoolIds: schoolIds},
                type:'POST',
                success:function (data) {
                    window.top.swal(
                        data['msg'],
                        '',
                        'success'
                    );
                    dataList.dataGridReload();
                },
                error:function (data) {
                    window.top.swal(
                        data['responseJSON']['error'],
                        '',
                        'error'
                    );
                }
            });
        },function () {
        })
    });
});
function getParams() {
    var key=xhExtra.getValue($('#list_search'));
    return {key:key,depId:currentDepId,depttype:currentDeptType,tabletype:currentTableType};
}